package com.aat.dxfy.filter.word;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KeyWordFilter {
	private static Pattern pattern = null;

	public static void main(String[] args) {
		String s = "王二丫是二逼吗";
		System.out.println(doFilter(s));
		System.out.println(hasKeywords(s));
	}

	private static void initPattern() {
		pattern = Pattern.compile("共产党|妈的|操|二逼");
	}

	/**
	 * @Description: 对关键字或敏感词进行过滤
	 */
	public static String doFilter(String str) {
		try {
			initPattern();
			if (null != pattern) {
				Matcher m = pattern.matcher(str);
				str = m.replaceAll("**");
			}
		} catch (Exception e) {
		}
		return str;
	}

	/**
	 * @Description: 敏感词或者关键字是否在词库中
	 */
	public static boolean hasKeywords(String str) {
		try {
			initPattern();
			if (null != pattern) {
				Matcher m = pattern.matcher(str);
				return m.find();
			}
		} catch (Exception e) {
		}
		return false;
	}
}
